package com.blackhorse.service;

import com.blackhorse.dao.AccountDao;
import com.blackhorse.domain.Account;

/**
 * 该service包下的所有实现类代码完全相同，只是为了不同声明式事务控制的测试用
 */
public class AccountServiceImpl2 implements AccountService {

    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public Account findAccountById(int id) {
        return accountDao.findAccountById(id);
    }

    @Override
    public void transfer(String sourceName, String targetName, float money) {
        Account srcAccount = accountDao.findAccountByName(sourceName);
        Account tarAccount = accountDao.findAccountByName(targetName);

        srcAccount.setMoney(srcAccount.getMoney() - money);
        tarAccount.setMoney(tarAccount.getMoney() + money);

        accountDao.updateAccount(srcAccount);
        //如果这里出现异常，转账金额将不再守恒
        int a = 1/0;
        accountDao.updateAccount(tarAccount);
    }
}
